//
//  NewsListThrController.swift
//  WxToutiao
//
//  Created by YQ05 on 2018/4/21.
//  Copyright © 2018年 yons. All rights reserved.
//

import UIKit

class NewsListThrController: UITableViewController {
    
    //var newsList7 = ["第一行数据","第二行数据","第三行数据","第四行数据"]
    var newsList7:[NewsData2] = []
    public var indextitle : String?
    public var textLabel: UILabel!
    
    var parentNavi : UINavigationController?
    var getId : Int = 0
    var id = 15

    

    override func viewDidLoad() {
        super.viewDidLoad()
        
          textLabel = UILabel(frame: self.view.bounds)

        self.view.addSubview(textLabel)
         //print(indextitle ?? "没有值")
         //print(getId ?? "getId没有值")
        
        getDataNLT(id:27)
        
        //newsList7.append(indextitle!)


        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }
    
    public func getDataNLT(id: Int)  {
        
        NewsData2.request(id: id) { (data) in
            
            
            if let data = data {
                
                OperationQueue.main.addOperation {
                    self.newsList7 = data
                    self.tableView.reloadData()
                    self.refreshControl?.endRefreshing()
                    
                    //print("newscalist  self.newsList7",self.newsList7)
                    print("OperationQueue数量news",self.newsList7.count)
                    
                }
                
                print("newscalist接收到的分类ID",self.self.getId)
                
            } else {
                print("NewsCateListController网络错误")
            }
        }
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableViewAutomaticDimension
       // tableView.reloadData()
        

    }
    
    public func setTitle(title: String) {
        textLabel.backgroundColor = UIColor.red
        textLabel.textColor = UIColor.black
        textLabel.textAlignment = .center
        self.view.addSubview(textLabel)
        
       // print("setTitle",title)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source



    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return newsList7.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NLTCell", for: indexPath) as!  NLTCell
        
        print("cellForRowAt")
        let news = newsList7[indexPath.row]
        print("newsList7数量news",newsList7.count)
        
        //cell.NewstitleLabel.text = newsList2[0]
        
        cell.NLTtitleLabel.text = news.title!
        print("NLT新闻标题",news.title!)
        cell.NLTdataLAbel.text = news.modified
        print("NLT新闻时间",news.modified as! String)
        //Configure the cell...
        
        return cell
       
        
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let newsdetail = newsList7[tableView.indexPathForSelectedRow!.row]
        
        let detailVC = storyboard?.instantiateViewController(withIdentifier: "SBidhNewsDetail") as! hNewsDetailController
        
        //detailVC.title = title! + "新闻详情"
        
        detailVC.title = newsdetail.title
        detailVC.post = newsdetail
        
        
        parentNavi?.pushViewController(detailVC, animated: true)
    }
 

    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
